var Bmob = require('../../utils/bmob.js');
var common = require('../../utils/common.js');
var app = getApp()
Page({
    data: {
        Car: [],
        CarIndex: 0,
        length: 0,
        username: null,
        verifySmsCode: '',
        openid: null,
        phone: '',
        equipId: null,
        description: null

    },
    onLoad: function (e) {
        var that = this
        //调用应用实例的方法获取全局数据
        var username = app.globalData.username;
        that.setData({
            username: username
        });
        console.log(username);

        // 跳转链接参数信息保存
        app.getUserInfo(function (userInfo) {
            console.log(userInfo);
            //更新数据
            that.setData({
                userId: e.userId,
                equipId: e.equipId,
                description: e.description
            })
        });

        // 获取当前用户的所拥有车牌
        wx.request({
          url: 'https://car.shiliangproject.cn/car/showcars?username='+username,
          data: {
            username: username
          },
          // 请求头
          header: {
            "Content-Type": 'application/json'
          },
          method: "GET",
          success: function (res) {
            var length = res['data'].length;
            console.log(res['data']);
            // 将当前用户未停在车位的车子筛选出来
            var car_temp = [];
            for (var i=0;i<length;i++) {
                if (res['data'][i]['status'] == 0) {
                  car_temp[i] = res['data'][i]['licence_plate'];
                }
            }
            that.setData({
              Car: car_temp,
              length: length
            });
            // console.log(that.data.username);
            // console.log(that.data.Car);
          },
          fail: function (err) { }, // 请求失败
          complete: function () { } // 请求完成后执行的函数
        });

    },

    // 下拉选择车牌触发事件
    bindPickerChange: function (e) {
      this.setData({
        CarIndex: e.detail.value
      })
    },

    sendSms: function (event) {
        var that = this
        var username = that.data.username;
        var equipId = event.detail.value.equipId;
        var licence_plate = that.data.Car[that.data.CarIndex];
        if (licence_plate != "" && equipId != "" && username!= "") {
  wx.request({
    url: 'https://car.shiliangproject.cn/equipment/reserve',
    data: {
      username: username,
      equipId: equipId,
      licence_plate: licence_plate
    },
    // 请求头
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    success: function (res) {
      console.log(res.data);
      if (res.data['res'] == 1) {
        common.showTip('预约成功！');
        setTimeout(function () {
          wx.switchTab({
            url: '../alltimeinfo/alltimeinfo'
          });
        }, 2000);
      }else {
        common.showTip('预约失败！');
      }
    }
  })
        }else {
          wx.showLoading({
            title: '车位信息错误！',
          });
          setTimeout(function() {
            wx.hideLoading();
          },1500)
        }


    },
    verifySmsCode: function (event) {
        var phone = this.data.phone;
        var verifyCode = event.detail.value.verifyCode;
        if (!phone) {
            common.showTip('请发送短信后再验证');
            return false;
        } else if (!verifyCode) {
            common.showTip('请输入验证码');
            return false;
        }

        Bmob.Sms.verifySmsCode(phone, verifyCode).then(function (obj) {
            common.showTip('验证成功' + "smsId:" + obj.msg);
            that.setData({
                verifySmsCode: "验证成功"
            })
        }, function (err) {
            common.showTip('验证失败' + err);
            that.setData({
                verifySmsCode: "验证失败"
            })
        });
    }
})